﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using G.Model;

namespace GameControls
{
    public class BoardShape
    {
        public event MouseEventHandler MouseClick;
        
        public enum FillStyle
        {
            Horizontal = 0,
            Solid = -1,
            Sphere = 47,
            Transparent = -2
        }
        
        public enum BackStyle
        {
            Transparent,
            Opaque
        }
    
        private Image _BackgroundImage;
        private Color _FillColor;
        private Color _BackColor;
        private int _Height;
        private int _Width;
        private int _X;
        private int _Y;
        private int _SquareSize;
        private Size _Size;
        private Point _Location;
        private string _Text;
        private object _Tag;
        private bool _DisplayCircle;
        private bool _UpperLeftCorner;
        private BoardShapeContainer _BoardShapeContainer;
        
        public BoardShape(BoardShapeContainer pBoardShapeContainer, int pSquareSize)
        {
            _BoardShapeContainer = pBoardShapeContainer;
            _Size = new Size(pSquareSize, pSquareSize);
            _SquareSize = pSquareSize;
            _Location = new Point(0, 0);
        }
        
        public bool DisplayCircle
        {
            get { return _DisplayCircle; }
            set 
            {
                if (_DisplayCircle != value)
                {
                    _DisplayCircle = value;
                    Invalidate();
                }
            }
        }
            
        public bool UpperLeftCorner
        {
            get { return _UpperLeftCorner; }
            set 
            {
                if (_UpperLeftCorner != value)
                {
                    _UpperLeftCorner = value;
                    Invalidate();
                }
            }
        }
        
        public object Tag
        {
            get { return _Tag; }
            set { _Tag = value; }
        }
        
        public Image BackgroundImage
        {
            get { return _BackgroundImage; }
            set 
            { 
                if (_BackgroundImage != value)
                {
                    _BackgroundImage = value; 
                }
            }
        }
        
        public Color FillColor
        {
            get { return _FillColor; }
            set { _FillColor = value; }
        }
        
        public Size Size
        {
            get { return _Size; }
            set { _Size = value; }
        }
        
        public Point Location
        {
            get { return _Location; }
            set { _Location = value; }
        }
        
        public Color BackColor
        {
            get { return _BackColor; }
            set { _BackColor = value; }
        }
        
        public int Height
        {
            get { return _Height; }
            set { _Height = value; }
        }
        
        public int Width
        {
            get { return _Width; }
            set { _Width = value; }
        }
        
        public int X
        {
            get { return _X; }
            set { _X = value; }
        }
        
        public int Y
        {
            get { return _Y; }
            set { _Y = value; }
        }
        
        public Rectangle GetSurface()
        {
            return new Rectangle(
                _Location.X, _Location.Y,
                _Size.Width - 1, _Size.Height - 1);
        }
        
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }
        
        public bool HitTest(Point pPoint)
        {
            Rectangle r = new Rectangle(_Location, _Size);
            return r.Contains(pPoint);
        }
        
        public void OnPaint(BoardShapeContainer sender, PaintEventArgs e)
        {
            Color color = Color.Black;
            if (_BoardShapeContainer.HeighlightedShape == this)
            {
                color = Color.Purple;
            }
            Pen pen = new Pen(color, 1);
            
            Graphics g = e.Graphics;
            
            Brush backBrush = new SolidBrush(_BackColor);
            g.FillRectangle(backBrush, GetSurface());
            
            if (_BoardShapeContainer.HeighlightedShape == this)
            {
                Pen highlightBrush = new Pen(Color.PeachPuff, 5);
                Rectangle inner = GetSurface();
                inner.Inflate(-5, -5);
                g.DrawRectangle(highlightBrush, inner);
            }
            
            Dictionary<string, Image> list = sender.GameUserControl.ImageDictionary;
            if (_BackgroundImage != null)
            {
                if (_BackgroundImage != null)
                {
                    g.DrawImage(_BackgroundImage, _Location.X + 2, _Location.Y + 2);
                }
            }
            
            if (_DisplayCircle) 
            {
                Pen highlightBrush = new Pen(Color.DarkTurquoise, 5);
                Rectangle inner = GetSurface();
                inner.Inflate(-15, -15);
                g.DrawEllipse(highlightBrush, inner);
            }
            
            if (_UpperLeftCorner) 
            {
                Brush blackBrush = new SolidBrush(Color.Black);
                Rectangle inner = GetSurface();
                g.FillRectangle(blackBrush, inner.X, inner.Y, 5, 5);
            }
            
            if (_Text != null && _Text.Length > 0) 
            {
                Font font = new Font(FontFamily.GenericMonospace, 12);
                Brush blackBrush = new SolidBrush(Color.Black);
                Rectangle inner = GetSurface();
                g.DrawString(_Text, font, blackBrush, inner.X, inner.Y);
            }
        }
        
        public void OnMouseClick(BoardShapeContainer sender, MouseEventArgs e) 
        {
            _BoardShapeContainer.HeighlightedShape = this;
            if (this.MouseClick != null)
            {
                MouseClick(sender, e);
            }
        }
        
        public void Invalidate()
        {
            _BoardShapeContainer.GameUserControl.Invalidate(GetSurface());
        }
        
    }
}
